package Gui;

import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JPanel;

import Gui.Dialogs.ViewDealCardsDialog;
import Gui.Dialogs.ViewLoansDialog;
import Gui.Dialogs.ViewLottoCard;
import Gui.Dialogs.ViewMailCardsDialog;
import Main.Defines;
import PlayerStates.FinalLoad;
import PlayerStates.InitialLoad;

public class ButtonActionPanel extends JPanel {
	/**
	 * 
	 */
	private JButton m_rollDiceEndTurn, m_viewDealCards, m_viewMailCards,
			m_viewLoans, m_quit, m_viewLotto;
	private static final long serialVersionUID = 1L;
	private static ButtonActionPanel ref;
	private static String ROLL_DICE_TEXT = "Roll Dice";
	private static String END_TURN_TEXT = "End Turn";

	private ActionListener getViewDealCardsListener() {
		return new ActionListener() {

			@Override()
			public void actionPerformed(ActionEvent e) {
				@SuppressWarnings("unused")
				ViewDealCardsDialog showDialog = new ViewDealCardsDialog();

			}
		};
	}

	private ActionListener getViewLoansListener() {
		return new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				@SuppressWarnings("unused")
				ViewLoansDialog showDialog = new ViewLoansDialog();

			}
		};
	}

	private ActionListener getQuitListener() {
		return new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO handle more properly by ending the game loop, or asking
				// if they are sure they want to quit
				System.exit(0);
			}
		};
	}

	private ActionListener getViewMailListener() {
		return new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				@SuppressWarnings("unused")
				ViewMailCardsDialog showDialog = new ViewMailCardsDialog();

			}
		};
	}

	private ActionListener getRollDiceEndTurnListener() {
		return new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {

				if (m_rollDiceEndTurn.getText().equals(ROLL_DICE_TEXT)) {
					Defines.EVM.queueEvent("EVENT_ROLL_DICE");
				}
				else if (m_rollDiceEndTurn.getText().equals(END_TURN_TEXT)) {
					Defines.EVM.queueEvent("EVENT_END_TURN");
				}

			}
		};
	}

	private ActionListener getViewLottoListener() {
		return new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				@SuppressWarnings("unused")
				ViewLottoCard showDialog = new ViewLottoCard();

			}
		};
	}

	private ButtonActionPanel() {
		setName("ButtonActionPanel");
		setLayout(new GridLayout(0, Defines.SINGLE_ROW_COLUMN_MODIFIER,
				Defines.EMPTY_BORDER_PADDING, Defines.EMPTY_BORDER_PADDING)); 

		m_rollDiceEndTurn = new JButton("Roll Dice");
		m_viewDealCards = new JButton("View Deal Cards");
		m_viewMailCards = new JButton("View Mail Cards");
		m_viewLoans = new JButton("View Loans");
		m_viewLotto = new JButton("View Lotto Card");
		m_quit = new JButton("Quit");

		m_rollDiceEndTurn.addActionListener(getRollDiceEndTurnListener());
		m_viewDealCards.addActionListener(getViewDealCardsListener());
		m_viewMailCards.addActionListener(getViewMailListener());
		m_viewLoans.addActionListener(getViewLoansListener());
		m_viewLotto.addActionListener(getViewLottoListener());
		m_quit.addActionListener(getQuitListener());

		this.add(m_rollDiceEndTurn);
		this.add(m_viewDealCards);
		this.add(m_viewMailCards);
		this.add(m_viewLoans);
		this.add(m_viewLotto);
		this.add(m_quit);
	}

	public void update() {
		if (Defines.CP().getCurrentState() == InitialLoad.getInstance()) {
			m_rollDiceEndTurn.setBackground(Color.green);
			m_rollDiceEndTurn.setText(ROLL_DICE_TEXT);
		}
		else if (Defines.CP().getCurrentState() == FinalLoad.getInstance()) {
			m_rollDiceEndTurn.setBackground(Color.red);
			m_rollDiceEndTurn.setText(END_TURN_TEXT);
		}

	}

	public static ButtonActionPanel getInstance() {
		if (ref == null) {
			ref = new ButtonActionPanel();
		}
		return ref;
	}

}
